/**
 * Moder是用来维护系统不同的功能状态的工具
 */
 

var FG;
if (!FG)
    FG = {};


FG.Moder = function() {
    var this_ = this;
    
    this.modes = {};
    this.currentMode = null;
    
};

FG.Moder.prototype.registerMode = function(modeName, enter, exit) {
    if (!_.has(this.modes, modeName)) {
        this.modes[modeName] = { enterFuncs: [], exitFuncs: [] };
    }
    if (_.isFunction(enter)) {
        this.modes[modeName].enterFuncs.push(enter);
    }
    
    if (_.isFunction(exit)) {
        this.modes[modeName].exitFuncs.push(exit);
    }
};

FG.Moder.prototype.toggleMode = function(modeName) {
    if (_.has(this.modes, modeName)) {
        if (modeName !== this.currentMode) {
            if (!!this.currentMode) {
                _.each(this.modes[this.currentMode].exitFuncs, function(f) {f()});
            }
            _.each(this.modes[modeName].enterFuncs, function(f) {f()});
            this.currentMode = modeName;
        }
    }
};

